package com.hqyj.controller;

import com.hqyj.dao.GoodsDao;
import com.hqyj.pojo.Goods;

import com.hqyj.pojo.PageData;
import com.hqyj.service.GoodsService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/shop")
public class GoodsController2 {

    String img=null;
    @Autowired
    private GoodsService2 goodsService2;
 //商品分页管理页面
    @RequestMapping("/goods")
    public String ManageGoods(Integer PageNumber, Integer PageSize, ModelMap model, String name) {

        if (PageNumber == null) {
            PageNumber = 1;
        }
        if (PageSize == null) {
            PageSize = 5;
        }
        if (name == null) {
            name = "";
        }

        ModelAndView mv = new ModelAndView();

        PageData pageData = goodsService2.showGoodsBypage(PageNumber, PageSize, name);
        List<Map<String,String>> maps = goodsService2.selectType();

        model.put("GoodsData", pageData);

        model.put("TypeMap",maps);


        return "goodShop";

    }
//修改商品信息
    @RequestMapping("goods/modify")
    @ResponseBody
    public Map<String, Object> ModifyGoodsById(Long goodsId, String goodsPic, String goodsName, String goodsDesc, Integer goodsStatus, Long typeId,
                                               String attributeList, double goodPrice, Long goodsStock, double discount, String createTime,
                                               String updateTime) {
        Map<String, Object> map = new HashMap<>();
        int i = goodsService2.ModifyGoodsById(goodsId, goodsPic, goodsName, goodsDesc, goodsStatus, typeId, attributeList, goodPrice, goodsStock, discount, createTime, updateTime);
        if (i >0) {

            map.put("message",1);
            return map;
        }
        return null;


    }

 //图片保存到本地文件 并上传图片名字到数据库
    @RequestMapping("/goods/savePicture")
    @ResponseBody
    public Map<String,Object> savaPicture(MultipartFile file,HttpServletRequest request,Long goodsId)  {
        Map<String,Object> map=new HashMap<>();
        System.out.println(goodsId);

        if(file!=null&&file.getOriginalFilename()!="") {
            final String filename = file.getOriginalFilename();
            final String suffer = filename.substring(filename.lastIndexOf("."));
            final UUID uuid = UUID.randomUUID();
            final String path = request.getServletContext().getRealPath("static");
            final File upload = new File(path, "images");
            if (!upload.exists()) {
                upload.mkdirs();
            }
             img = uuid + suffer;
            final File imgFile = new File(upload.getAbsolutePath(), img);
            //修改数据库名字
            int i = goodsService2.updatePicture(img, goodsId);
            System.out.println(i);
            try {
                file.transferTo(imgFile);
                map.put("message",1);
            } catch (IOException e) {
                e.printStackTrace();
                map.put("message","error");
            }
        }
        return map;
    }


    //商品购买页面
@RequestMapping("/sale")

    public String goodsSalePage(ModelMap model){

    List<Goods> allGoods = goodsService2.getAllGoods();
    System.out.println(allGoods);
    model.put("goodslist",allGoods);


    return "goodsale";
    }





}
